Skip to content

initial stab at a zeroconf unified event stream#2

Merged
jmattsson merged 10 commits into
mainfrom
add-zeroconf-event-stream
Jun 12, 2026
Merged

initial stab at a zeroconf unified event stream#2
jmattsson merged 10 commits into
mainfrom
add-zeroconf-event-stream

Conversation

@bookman-dius

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread src/powersensor_local/devices.py Outdated
return # no change
# Address changed — disconnect stale connection and reconnect.
await api.disconnect()
self._plug_apis.pop(mac)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a race here, since the await may block for some time and conceivably we'd get _plug_discovered() called again with the same mac prior to the first disconnect finishing? If so, we should pop() it before we await, yes?

async def _plug_lost(self, mac: str) -> None:
"""Called by the subclass when a plug has definitively disappeared."""
if mac in self._plug_apis:
await self._plug_apis.pop(mac).disconnect()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this does!

await api.disconnect()
self._plug_apis.pop(mac)

await self._add_device(mac, 'plug')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a prior self._remove_device() if we already had it? To keep events symmetrical?

await self._add_device(mac, 'sensor')
if self._relay_now_relaying_for and self._event_cb is not None:
obj['event'] = ev
await self._event_cb(obj)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not need a device.subscribed check?


async def yourcallback(event: dict) -> None

Known events:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did the documentation for known events end up after the refactor?

Usage example (HA — pass the shared zeroconf instance)::

zc = await homeassistant.components.zeroconf.async_get_instance(hass)
devices = PowersensorZeroconfDevices(zeroconf_instance=zc)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__init__ does not seem to take zeroconf_instance as a named parameter, just a positional one?

self._start_expiry_timer()

if self._zc_instance is None:
self._zc_instance = _zc.Zeroconf()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not initialisation that belongs in __init__()?

self._zc_instance = _zc.Zeroconf()

loop = asyncio.get_running_loop()
self._listener = _Listener(self, loop)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a check to handle the case where someone calls start() multiple times? Aren't we just leaving prior instances dangling otherwise?

info = _zc.ServiceInfo(type_, name)
if not info.load_from_cache(zc):
_LOGGER.warning(
"No cache entry for %s — device will appear on next mDNS announcement",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this bad? Can it be avoided? What would I need to do differently to avoid scary log messages?

To use mDNS-based discovery, install the zeroconf extra::

pip install powersensor-local[zeroconf]
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, fancy and user friendly!

@jmattsson jmattsson force-pushed the add-zeroconf-event-stream branch from bcaf15c to 06d906c Compare June 12, 2026 01:17
@jmattsson jmattsson merged commit 25a60f3 into main Jun 12, 2026
1 check passed
@jmattsson jmattsson deleted the add-zeroconf-event-stream branch June 12, 2026 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants